home *** CD-ROM | disk | FTP | other *** search
/ Network Support Library / RoseWare - Network Support Library.iso / apidev / mhs_c.arc / INPOST.ARC / PARSE.C < prev   
C/C++ Source or Header  |  1988-06-27  |  2KB  |  103 lines

  1. /* ******************************** PARSE.C ******************************* */
  2. #include "cctypes.h"
  3.  
  4. #define LINE_LEN_MAX 81
  5. #define FIRST_WORD_SIZE 37
  6. char currentLine[LINE_LEN_MAX];
  7.  
  8. extern int InFile;
  9. extern int comType;
  10. extern char messageBuffer[];
  11.  
  12. void UpperCase();
  13. void AddCurrentLineToMessageBuffer();
  14. int GetNextLine();
  15. void GetFirstWord();
  16.  
  17. int Parse()
  18. {
  19.     int returnCode = NO_ERROR, ccode, bytes = 0;
  20.     char firstWord[FIRST_WORD_SIZE];
  21.  
  22.     while (1) { /* as long as we're getting bytes... */
  23.         bytes = GetNextLine(); /* uses InFile and currentLine */
  24.         if ( bytes == 0 ) /* EOF */
  25.             break;
  26.         /* else */
  27.  
  28.         GetFirstWord(firstWord); /* gets the first word of the current line */
  29.  
  30.         ccode = GetCommandType(firstWord);
  31.         if ( ccode != -1 )
  32.             CreateMCBLine(ccode); /* ccode is the switch table index */
  33.         else 
  34.             AddCurrentLineToMessageBuffer();
  35.  
  36.     } /* end while */
  37.     return(returnCode);
  38. }
  39.  
  40. void AddCurrentLineToMessageBuffer()
  41. {
  42.     char temp[135];
  43.  
  44.     sprintf(temp, "%s\15\12", currentLine);
  45.     strcat(messageBuffer, temp);
  46. }
  47.  
  48. int GetNextLine()
  49. {
  50.     int bytesRead = 0, temp;
  51.     char ch[1];
  52.  
  53.     ch[0] = ' ';
  54.     currentLine[0] = '\0';
  55.     while (ch[0] != 13) {
  56.         temp = read(InFile, ch, 1);
  57.         if ( temp == 0 )
  58.             break;
  59.         currentLine[bytesRead++] = ch[0];
  60.     }
  61.     if ( temp )
  62.         temp = read(InFile, ch, 1); /* ch should be 10 here */
  63.     currentLine[bytesRead-1] = '\0'; /* null terminate where we got the 13 */
  64.     return(bytesRead);
  65. }
  66.  
  67. void GetFirstWord(firstWord)
  68. char *firstWord;
  69. {
  70.     firstWord[0] = '\0';
  71.  
  72.     stptok(currentLine, firstWord, FIRST_WORD_SIZE, " ");
  73. }
  74.              
  75. void UpperCase(s)
  76. char *s;
  77. {
  78.     int i = 0;
  79.  
  80.     while ( s[i] != '\0' ) {
  81.         s[i] = islower(s[i]) ? toupper(s[i]) : s[i];
  82.         i++;
  83.     }
  84. }
  85.  
  86. int GetCommandType(command)
  87. char *command;
  88. {
  89.     int i;
  90.  
  91.     if ( strlen(command) ) {
  92.         for ( i = 0; CommandTypes[i]; i++ ) {
  93.             if ( strcmp(command, CommandTypes[i]) == 0 ) {
  94.                 comType = i;
  95.                 return(i);
  96.             }
  97.         }
  98.     }
  99.     return(BADCOMMAND);
  100. }
  101.  
  102.  
  103.